package com.neuedu.jdboot.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.neuedu.jdboot.admin.interceptor.LoginInterceptor;

/**
 * 
 * @author chens
 *
 */
@Configuration
public class MvcConfig {
	private Logger logger = LoggerFactory.getLogger(MvcConfig.class);

	@Value("${web.upload.path}")
	private String uploadPath;

	@Bean
	public WebMvcConfigurer webMvcConfigurer() {
		return new WebMvcConfigurer() {
			@Override
			public void addResourceHandlers(ResourceHandlerRegistry registry) {
				logger.info(uploadPath);
				registry.addResourceHandler("/upload/**").addResourceLocations("file:" + uploadPath);
			}

			@Override
			public void addInterceptors(InterceptorRegistry registry) {

				// 注册TestInterceptor拦截器
				InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());
				registration.addPathPatterns("/sys/**"); // 只拦截后台页面，且静态资源不拦截

				registration.excludePathPatterns( // 添加不拦截路径
						"/sys/login", // 登录
						"/sys/validatecode", // 验证码不能拦
						"/upload/**" // 上传目录不拦截
				);
			}
		};
	}

}